package com.souku.finance.application.ui.mine;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.manager.RequestManager;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.souku.finance.application.R;
import com.souku.finance.application.adapter.AddpointAdapter;
import com.souku.finance.application.adapter.RedpacketAdapter;
import com.souku.finance.application.adapter.ViewPagerAdapter;
import com.souku.finance.application.base.BaseActivity;
import com.souku.finance.application.bean.AddpointBean;
import com.souku.finance.application.bean.RedpacketBean;
import com.souku.finance.application.http.UrlConstact;
import com.souku.finance.application.utils.Constant;
import com.souku.finance.application.utils.JsonUtil;
import com.souku.finance.application.utils.ParamUtil;
import com.souku.finance.application.utils.ShareUtil;
import com.souku.finance.application.utils.ToastUtil;
import com.souku.finance.application.widget.ViewPagerIndicator;
import com.souku.finance.application.http.volley.ReqListener;
import com.souku.finance.application.http.volley.Success;
import com.souku.finance.application.http.volley.SuccessError;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by Android on 2016/12/20.
 */

public class DiscountActivity extends BaseActivity implements View.OnClickListener {

    protected ViewPagerIndicator idIndicator;
    protected TextView tvRedCount;
    protected TextView tvAddCount;
    private PullToRefreshListView addpoint_listview;
    private AddpointAdapter addpointAdapter;
    private List<AddpointBean.ListBean> addpointlist = new ArrayList<>();

    private PullToRefreshListView redpacket_listview;
    private RedpacketAdapter redpacketAdapter;
    private List<RedpacketBean.ListBean> redlist = new ArrayList<>();

    protected RelativeLayout topBack;
    protected TextView topTitle;
    protected ViewPager idVp;
    private View view1;
    private View view2;
    private int redpage = 1;
    private int pointpage = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.act_discount);
        initView();
        view1 = LayoutInflater.from(this).inflate(R.layout.page_redpacket, null);
        view2 = LayoutInflater.from(this).inflate(R.layout.page_addpoint, null);
        final ArrayList<View> views = new ArrayList<>();
        views.add(view1);
        views.add(view2);
        ViewPagerAdapter adapter = new ViewPagerAdapter(views);
        idVp.setAdapter(adapter);
        topTitle.setText("我的券包");

        redpacket_listview = (PullToRefreshListView) view1.findViewById(R.id.redpacket_pull_list);
        redpacketAdapter = new RedpacketAdapter(redlist);
        redpacket_listview.setMode(PullToRefreshBase.Mode.BOTH);
        redpacket_listview.setAdapter(redpacketAdapter);
        redpacket_listview.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                redlist.clear();
                redpage = 1;
                getEcv_list();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                redpage++;
                getEcv_list();
            }
        });

        addpoint_listview = (PullToRefreshListView) view2.findViewById(R.id.addpoint_pull_list);
        addpointAdapter = new AddpointAdapter(addpointlist);
        addpoint_listview.setMode(PullToRefreshBase.Mode.BOTH);
        addpoint_listview.setAdapter(addpointAdapter);
        addpoint_listview.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                addpointlist.clear();
                pointpage = 1;
                getPoint_list();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                pointpage++;
                getPoint_list();
            }
        });

        idIndicator.setViewPager(idVp, 0);
    }

    @Override
    public void internet() {
        redpacket_listview.setRefreshing(true);
        addpoint_listview.setRefreshing(true);
        getPoint_count();
        getRed_count();
    }

    @Override
    protected void onResume() {
        super.onResume();
        redpacket_listview.setRefreshing(true);
        addpoint_listview.setRefreshing(true);
        getPoint_count();
        getRed_count();
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.top_back) {
            finish();
        }
    }

    private void initView() {
        topBack = (RelativeLayout) findViewById(R.id.top_back);
        topBack.setOnClickListener(DiscountActivity.this);
        topTitle = (TextView) findViewById(R.id.top_title);
        idVp = (ViewPager) findViewById(R.id.id_vp);
        idIndicator = (ViewPagerIndicator) findViewById(R.id.id_indicator);
        tvRedCount = (TextView) findViewById(R.id.tv_red_count);
        tvAddCount = (TextView) findViewById(R.id.tv_add_count);
    }


    private void getEcv_list() {
        HashMap<String, String> map = ParamUtil.getParams();
        map.put("pgNo", redpage + "");
        map.put("userId", ShareUtil.getInstance().getString(Constant.UserId));
        map.put("pgSize", "10");
        RequestManager.getInstance().post(UrlConstact.ECV_LIST, ParamUtil.getSing(map), new ReqListener(this, new SuccessError() {
            @Override
            public void onSuccess(String response) {
                redpacket_listview.onRefreshComplete();
                if (JsonUtil.getCode(response) == 1) {
                    RedpacketBean bean = JsonUtil.fastBean(JsonUtil.getData(response).toString(), RedpacketBean.class);
                    if (bean.getList().size() == 0) {
                        ToastUtil.show("没有更多内容");
                    } else {
                        redlist.addAll(bean.getList());
                        redpacketAdapter.notifyDataSetChanged();
                    }
                } else {
                    ToastUtil.show(JsonUtil.getMessage(response));
                }
            }

            @Override
            public void onError(String errorMsg) {
                ToastUtil.show("请求失败");
                redpacket_listview.onRefreshComplete();
            }
        }), ParamUtil.getHeaders());
    }


    private void getPoint_list() {
        HashMap<String, String> map = ParamUtil.getParams();
        map.put("pgNo", pointpage + "");
        map.put("userId", ShareUtil.getInstance().getString(Constant.UserId));
        map.put("pgSize", "10");
        map.put("irc_st", "-1");
        RequestManager.getInstance().post(UrlConstact.QUERY_PROM_IRC, ParamUtil.getSing(map), new ReqListener(this, new SuccessError() {
            @Override
            public void onSuccess(String response) {
                addpoint_listview.onRefreshComplete();
                if (JsonUtil.getCode(response) == 1) {
                    AddpointBean bean = JsonUtil.fastBean(JsonUtil.getData(response).toString(), AddpointBean.class);
                    if (bean.getList().size() == 0) {
                        ToastUtil.show("没有更多内容");
                    } else {
                        addpointlist.addAll(bean.getList());
                        addpointAdapter.notifyDataSetChanged();
                    }
                } else {
                    ToastUtil.show(JsonUtil.getMessage(response));
                }
            }

            @Override
            public void onError(String errorMsg) {
                ToastUtil.show("请求失败");
                addpoint_listview.onRefreshComplete();
            }
        }), ParamUtil.getHeaders());
    }

    private void getPoint_count() {
        HashMap<String, String> map = ParamUtil.getParams();
        RequestManager.getInstance().post(UrlConstact.COUNT_AVAILIBLE_PROM_IRC, ParamUtil.getSing(map), new ReqListener(this, new Success() {
            @Override
            public void onSuccess(String response) {
                if (JsonUtil.getCode(response) == 1) {
                    String count = JsonUtil.getString(JsonUtil.getJSONObject(response), "data");
                    tvAddCount.setText("加息券(" + count + ")");
                } else {
                    ToastUtil.show(JsonUtil.getMessage(response));
                }
            }
        }), ParamUtil.getHeaders());
    }

    private void getRed_count() {
        HashMap<String, String> map = ParamUtil.getParams();
        RequestManager.getInstance().post(UrlConstact.ECV_COUNT, ParamUtil.getSing(map), new ReqListener(this, new Success() {
            @Override
            public void onSuccess(String response) {
                if (JsonUtil.getCode(response) == 1) {
                    String count = JsonUtil.getString(JsonUtil.getJSONObject(response), "data");
                    tvRedCount.setText("红包(" + count + ")");
                } else {
                    ToastUtil.show(JsonUtil.getMessage(response));
                }
            }
        }), ParamUtil.getHeaders());
    }
}
